﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Brain.Rendering
{
    public class Shape_BoundingSphere : Shape
    {
        public int SphereResolution { get; set; }

        public int SphereLineCount { get; set; }

        public Vector3[] UnitSphere { get; protected set; }

        public VertexPositionColor[] Vertices { get; set; }

        public bool UseVertexColor { get; set; }

        public Shape_BoundingSphere()
        {
            SphereResolution = 32;
            SphereLineCount = (SphereResolution + 1) * 3;

            UseVertexColor = true;

            InitializeSphere();
        }

        /// <summary>
        /// Creates the unitSphere array of vertices.
        /// </summary>
        public void InitializeSphere()
        {
            // We need two vertices per line, so we can allocate our vertices
            UnitSphere = new Vector3[SphereLineCount * 2];
            Vertices = new VertexPositionColor[UnitSphere.Length];

            // Compute our step around each circle
            float step = MathHelper.TwoPi / SphereResolution;

            // Used to track the index into our vertex array
            int index = 0;

            // Create the loop on the XY plane first
            for (float a = 0f; a < MathHelper.TwoPi; a += step)
            {
                UnitSphere[index++] = new Vector3((float)Math.Cos(a), (float)Math.Sin(a), 0f);
                UnitSphere[index++] = new Vector3((float)Math.Cos(a + step), (float)Math.Sin(a + step), 0f);
            }

            // Next on the XZ plane
            for (float a = 0f; a < MathHelper.TwoPi; a += step)
            {
                UnitSphere[index++] = new Vector3((float)Math.Cos(a), 0f, (float)Math.Sin(a));
                UnitSphere[index++] = new Vector3((float)Math.Cos(a + step), 0f, (float)Math.Sin(a + step));
            }

            // Finally on the YZ plane
            for (float a = 0f; a < MathHelper.TwoPi; a += step)
            {
                UnitSphere[index++] = new Vector3(0f, (float)Math.Cos(a), (float)Math.Sin(a));
                UnitSphere[index++] = new Vector3(0f, (float)Math.Cos(a + step), (float)Math.Sin(a + step));
            }
        }

        public void Draw(object obj, Color color, ICamera3D Camera, ShapeDrawer drawer, Effect effect)
        {
            BoundingSphere sphere = (BoundingSphere)obj;

            for (int i = 0; i < Vertices.Length; i++)
            {
                Vector3 pos = (UnitSphere[i] * sphere.Radius) + sphere.Center;

                Vertices[i] = new VertexPositionColor(pos, color);
            }

            for (int i = 0; i < effect.CurrentTechnique.Passes.Count; i++)
            {
                EffectPass pass = effect.CurrentTechnique.Passes[i];

                pass.Apply();
                GraphicsDevice device = Engine.Instance.GraphicsDevice;
                device.DrawUserPrimitives(PrimitiveType.LineList, Vertices, 0, Vertices.Length / 2);
            }
        }
    }
}
